package com.xinxin.intelligenceexam.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.xinxin.intelligenceexam.R;
import com.xinxin.intelligenceexam.base.MyApplication;
import com.xinxin.intelligenceexam.domain.Question;
import com.xinxin.intelligenceexam.domain.Reply;
import com.xinxin.intelligenceexam.utils.view.ImageLoader;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExamPagerFragment extends Fragment {
    private static final String PARAM_QUESTION = "question_param";
    private static final String PARAM_INDEX = "index_param";
    private static final String CHOOSE_INDEX = "choose_index";
    @BindView(R.id.tv_index)
    TextView mTvIndex;
    @BindView(R.id.tv_question)
    TextView mTvQuestion;
    @BindView(R.id.iv_question_pic)
    ImageView mIvQuestionPic;
    @BindView(R.id.rdbt_A)
    RadioButton mRdbtA;
    @BindView(R.id.rdbt_B)
    RadioButton mRdbtB;
    @BindView(R.id.rdbt_C)
    RadioButton mRdbtC;
    @BindView(R.id.rdbt_D)
    RadioButton mRdbtD;
    @BindView(R.id.radiogroup_answer)
    RadioGroup mRadiogroupAnswer;

    private Question mQuestion;
    private int mIndex;
    private int mChooseAnswer = 0;//0表示未选择，1表示选择A，2表示B，3表示C，4表示


    public ExamPagerFragment() {
        // Required empty public constructor
    }

    public static ExamPagerFragment newInstance(int index, Question question) {
        ExamPagerFragment fragment = new ExamPagerFragment();
        Bundle args = new Bundle();
        args.putInt(PARAM_INDEX, index);
        args.putParcelable(PARAM_QUESTION, question);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mQuestion = getArguments().getParcelable(PARAM_QUESTION);
            mIndex = getArguments().getInt(PARAM_INDEX);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exam_pager, container, false);
        ButterKnife.bind(this, view);
        if (savedInstanceState != null) {
            mChooseAnswer = savedInstanceState.getInt(CHOOSE_INDEX, 0);
        }
        initView();
        return view;
    }

    private void initView() {
        //初始化序号
        mTvIndex.setText(mIndex);
        //初始化内容
        if (mQuestion != null) {
            mTvQuestion.setText(mQuestion.getContent());
            if (mQuestion.getHasPic() != 0) {//有照片
                ImageLoader.getInstance().displayImage(getActivity(), mQuestion.getPicUrl(), mIvQuestionPic);
            }
        }
        //初始化4个按钮
        switch (mChooseAnswer) {
            case 0:
                break;
            case 1:
                mRdbtA.setChecked(true);
                break;
            case 2:
                mRdbtB.setChecked(true);
                break;
            case 3:
                mRdbtC.setChecked(true);
                break;
            case 4:
                mRdbtD.setChecked(true);
                break;
        }
    }


    @Override
    public void onDetach() {
        super.onDetach();
    }

    @OnClick({R.id.iv_question_pic, R.id.rdbt_A, R.id.rdbt_B, R.id.rdbt_C, R.id.rdbt_D})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_question_pic:
                break;
            case R.id.rdbt_A:
                addReply(1);
                break;
            case R.id.rdbt_B:
                addReply(2);
                break;
            case R.id.rdbt_C:
                addReply(3);
                break;
            case R.id.rdbt_D:
                addReply(4);
                break;
        }
    }

    private void addReply(int answer){
        Reply reply = new Reply();
        reply.setAnswer(answer);
        reply.setQuestion(mQuestion);
        MyApplication.getInstance().addReply(mIndex,new Reply());
    }
    /**
     * 用来恢复fragment的视图内容
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(CHOOSE_INDEX, mChooseAnswer);
    }
}
